{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "HDF_dataset_adds_on is intended for adding feature values to the HDF files, in a way to eliminate further computations. Features are added in the form of dataframes. Each dataframe has the structure [rows: the readable ECG leads, columns: feature values over the ECG ROI's]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import h5py\n",
    "import neurokit2 as nk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ECG_ID</th>\n",
       "      <th>Age</th>\n",
       "      <th>Age_class_0</th>\n",
       "      <th>Age_class_1</th>\n",
       "      <th>Age_class_2</th>\n",
       "      <th>Age_class_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A00002</td>\n",
       "      <td>32</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A00003</td>\n",
       "      <td>63</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A00006</td>\n",
       "      <td>46</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A00008</td>\n",
       "      <td>32</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A00009</td>\n",
       "      <td>48</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13900</th>\n",
       "      <td>A25755</td>\n",
       "      <td>44</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13901</th>\n",
       "      <td>A25756</td>\n",
       "      <td>76</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13902</th>\n",
       "      <td>A25757</td>\n",
       "      <td>55</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13903</th>\n",
       "      <td>A25764</td>\n",
       "      <td>20</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13904</th>\n",
       "      <td>A25765</td>\n",
       "      <td>52</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>13905 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       ECG_ID  Age  Age_class_0  Age_class_1  Age_class_2  Age_class_3\n",
       "0      A00002   32            2            1            0            0\n",
       "1      A00003   63            5            2            1            1\n",
       "2      A00006   46            3            1            1            0\n",
       "3      A00008   32            2            1            0            0\n",
       "4      A00009   48            3            1            1            0\n",
       "...       ...  ...          ...          ...          ...          ...\n",
       "13900  A25755   44            3            1            1            0\n",
       "13901  A25756   76            6            3            2            1\n",
       "13902  A25757   55            4            2            1            1\n",
       "13903  A25764   20            1            0            0            0\n",
       "13904  A25765   52            4            2            1            1\n",
       "\n",
       "[13905 rows x 6 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "normal_ecg_age = pd.read_pickle('normal_ecg_age.pickle')\n",
    "normal_ecg_age"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create h5py dataframe of R peaks: f['ECG_R_Peaks'][...]. Procedure also creates pickles files containing:\n",
    " - ecg_signal_read_error: pickle file containing ['ECG_ID', 'Derivation'] of signal read error OSError.\n",
    " - ecg_multiple_r_peaks_detection: pickle file containing ['ECG_ID', 'Derivation'] of signal with multiple R Peaks detection. (Un apagon interrumpio su calculo)\n",
    " - ecg_r_peaks_missing: pickle file containing ['ECG_ID', 'Derivation'] of signal with miss detection and -1 included because NaN is of type float.\n",
    "\n",
    "Dataframe in the form rows: derivations, columns: ECG_R_Peaks fiducial points."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "ecg_signal_read_error = pd.DataFrame(columns=['ECG_ID', 'Derivation'])\n",
    "ecg_multiple_r_peaks_detection = pd.DataFrame(columns=['ECG_ID', 'Derivation'])\n",
    "ecg_r_peaks_missing = pd.DataFrame(columns=['ECG_ID', 'Derivation'])\n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "    with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "        for der in range (12):\n",
    "            try:\n",
    "                ecg_sig = f['ecg'][der]\n",
    "            except OSError:\n",
    "                print(f'ECG signal: {id}, derivation: {der}. Couldnot be read')\n",
    "                ecg_signal_read_error.loc[len(ecg_signal_read_error)] = [id, der]\n",
    "                continue\n",
    "            ecg_fixed, is_inverted = nk.ecg_invert(ecg_sig, sampling_rate=500)\n",
    "            if is_inverted:\n",
    "                ecg_sig = ecg_fixed    \n",
    "            signals, _ = nk.ecg_process(ecg_sig, sampling_rate=500)\n",
    "            roi_ref = list(signals[signals['ECG_R_Peaks'] == 1].index)\n",
    "            if der == 0:\n",
    "                ECG_R_Peaks_dataframe = pd.DataFrame(columns=[c for c in range(len(roi_ref))])            \n",
    "            else:\n",
    "                while len(roi_ref) > len(ECG_R_Peaks_dataframe.columns):\n",
    "                    interval_difference = [0] * (len(roi_ref) - 1)\n",
    "                    for i in range(len(roi_ref) - 1):\n",
    "                        interval_difference[i] = roi_ref[i + 1] - roi_ref[i]\n",
    "                    index_min_interval = interval_difference.index(min(interval_difference)) + 1\n",
    "                    ecg_multiple_r_peaks_detection.loc[len(ecg_multiple_r_peaks_detection)] = [id, der]\n",
    "                    roi_ref.pop(index_min_interval)\n",
    "                while len(roi_ref) < len(ECG_R_Peaks_dataframe.columns):\n",
    "                    roi_ref.append(-1)\n",
    "                    ecg_r_peaks_missing.loc[len(ecg_r_peaks_missing)] = [id, der]\n",
    "            ECG_R_Peaks_dataframe.loc[len(ECG_R_Peaks_dataframe)] = roi_ref\n",
    "        #ECG_R_Peaks_dataframe.index = [['I', 'II', 'III', 'aVR', 'aVL', 'aVF', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6']]\n",
    "        f['ECG_R_Peaks'] = ECG_R_Peaks_dataframe\n",
    "        del ECG_R_Peaks_dataframe\n",
    "        f.close()\n",
    "ecg_signal_read_error.to_pickle('ecg_signal_read_error.pickle')\n",
    "ecg_multiple_r_peaks_detection.to_pickle('ecg_multiple_r_peaks_detection.pickle')\n",
    "ecg_r_peaks_missing.to_pickle('ecg_r_peaks_missing.pickle')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Because blackout running was interrupted and pickle files were not correctly created. The folloeing procedure is to create pickle files related with ECG read error and missing R peaks. The statistics related with multiple R peaks detection needs to be calculated on running time and the process is tedious (+24 hours)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ecg_signal_read_error = pd.DataFrame(columns=['ECG_ID', 'Derivation'])\n",
    "ecg_r_peaks_missing = pd.DataFrame(columns=['ECG_ID', 'Derivation'])\n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "    with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "        r_peaks = f['ECG_R_Peaks'][...]\n",
    "        row, col = r_peaks.shape\n",
    "        for peaks_list in r_peaks:\n",
    "            for c in range(col):\n",
    "                if peaks_list[c] == -1:\n",
    "                    ecg_r_peaks_missing.loc[len(ecg_r_peaks_missing)] = [id, der]\n",
    "        for der in range (12):\n",
    "            try:\n",
    "                ecg_sig = f['ecg'][der]\n",
    "            except OSError:\n",
    "                print(f'ECG signal: {id}, derivation: {der}. Couldnot be read')\n",
    "                ecg_signal_read_error.loc[len(ecg_signal_read_error)] = [id, der]\n",
    "                continue      \n",
    "        f.close()\n",
    "ecg_signal_read_error.to_pickle('ecg_signal_read_error.pickle')\n",
    "ecg_r_peaks_missing.to_pickle('ecg_r_peaks_missing.pickle')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Following procedure creates h5py dataframe of the fractal dimension values obtained around detected ECG R Peaks (ROI = [-150 + ECG_R_Peaks: 150 + ECG_R_Peaks]). \n",
    "Fractal functions are defined in fractal_function_list.\n",
    "Dataframe in the form: [rows: derivations, columns: fractal dimension values for each ECG ROI]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "count_wrong_read_signal = 0\n",
    "count_missing_peaks = 0\n",
    "fractal_function_list = [nk.fractal_higuchi, nk.fractal_hurst, nk.fractal_dfa]\n",
    "for fractal_functions in fractal_function_list:\n",
    "    for id in normal_ecg_age['ECG_ID']:\n",
    "        with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "            r_peaks = f['ECG_R_Peaks'][...]\n",
    "            col = r_peaks.shape[1]\n",
    "            temp_DataFrame = pd.DataFrame(columns=[c for c in range(col)])\n",
    "            temp_list = [np.NaN]*col\n",
    "            index = range(12)\n",
    "            for index, r in zip(index, r_peaks):\n",
    "                try:\n",
    "                    signal = f['ecg'][index]\n",
    "                except OSError:\n",
    "                    count_wrong_read_signal+=1\n",
    "                    continue\n",
    "                for c in range(col):\n",
    "                    if r[c] != -1:\n",
    "                        temp_list[c],_ = fractal_functions(signal[r[c] - 150:r[c] + 150])\n",
    "                    else:\n",
    "                        count_missing_peaks+=1\n",
    "                        temp_list[c] = temp_list[c - 1]\n",
    "                temp_DataFrame.loc[len(temp_DataFrame)] = temp_list\n",
    "                temp_list = [np.NaN]*col\n",
    "            mn = pd.Series(np.mean(temp_DataFrame, axis=1))\n",
    "            mn.name = col\n",
    "            std = pd.Series(np.std(temp_DataFrame, axis=1))\n",
    "            std.name = col + 1\n",
    "            temp_DataFrame = pd.concat([temp_DataFrame, mn, std], axis=1)\n",
    "            name = str(fractal_functions)\n",
    "            name = name.split(' ')\n",
    "            f[f'Fractal_Dimension/{name[1]}'] = temp_DataFrame\n",
    "            del temp_DataFrame, r_peaks, mn, std\n",
    "            f.close()\n",
    "    #print('Cannot read de signal:', count_wrong_read_signal) size of r_peaks don't let code to rest of derivations\n",
    "    print('Missing R peaks:', count_missing_peaks)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Find the derivations with minor Katz fractal dimension standard deviation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "derivations_list = [] \n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "    with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "        k = f['Katz_fractal'][:,-1]\n",
    "        derivations_list.append(np.argmin(k))\n",
    "np.save('derivations_list_Katz_std_minimun.npy.npy', derivations_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[   0  569]\n",
      " [   1  876]\n",
      " [   2  303]\n",
      " [   3  711]\n",
      " [   4  118]\n",
      " [   5  518]\n",
      " [   6  802]\n",
      " [   7 2107]\n",
      " [   8 1572]\n",
      " [   9 2473]\n",
      " [  10 2420]\n",
      " [  11 1436]]\n"
     ]
    }
   ],
   "source": [
    "unique, counts = np.unique(derivations_list, return_counts=True)\n",
    "print(np.asarray((unique, counts)).T)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Derivation ['I', 'II', 'III', 'aVR', 'aVL', 'aVF', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6'] with minor standard deviation: 9 corresponding to V4, 10 corresponding to V5, 7 corresponding to V2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "Katz_mean_457 = pd.DataFrame(columns=['ECG_ID', 'Katz_mean_V4', 'Katz_mean_V5', 'Katz_mean_V2'])\n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "    with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "        try:\n",
    "            Katz_mean_457.loc[len(Katz_mean_457)] = [id, f['Katz_fractal'][9,-2], f['Katz_fractal'][10,-2], f['Katz_fractal'][7,-2]]\n",
    "        except IndexError:\n",
    "            continue\n",
    "Katz_mean_457.to_pickle('Katz_mean_457.pickle')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ECG_ID</th>\n",
       "      <th>Katz_mean_V4</th>\n",
       "      <th>Katz_mean_V5</th>\n",
       "      <th>Katz_mean_V2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A00002</td>\n",
       "      <td>1.256836</td>\n",
       "      <td>1.205078</td>\n",
       "      <td>1.245117</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A00003</td>\n",
       "      <td>1.423828</td>\n",
       "      <td>1.388672</td>\n",
       "      <td>1.340820</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A00006</td>\n",
       "      <td>1.310547</td>\n",
       "      <td>1.313477</td>\n",
       "      <td>1.375000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A00008</td>\n",
       "      <td>1.284180</td>\n",
       "      <td>1.262695</td>\n",
       "      <td>1.316406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A00009</td>\n",
       "      <td>1.292969</td>\n",
       "      <td>1.264648</td>\n",
       "      <td>1.320312</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13862</th>\n",
       "      <td>A25755</td>\n",
       "      <td>1.243164</td>\n",
       "      <td>1.220703</td>\n",
       "      <td>1.407227</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13863</th>\n",
       "      <td>A25756</td>\n",
       "      <td>1.233398</td>\n",
       "      <td>1.227539</td>\n",
       "      <td>1.317383</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13864</th>\n",
       "      <td>A25757</td>\n",
       "      <td>1.362305</td>\n",
       "      <td>1.320312</td>\n",
       "      <td>1.367188</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13865</th>\n",
       "      <td>A25764</td>\n",
       "      <td>1.291992</td>\n",
       "      <td>1.266602</td>\n",
       "      <td>1.439453</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13866</th>\n",
       "      <td>A25765</td>\n",
       "      <td>1.243164</td>\n",
       "      <td>1.222656</td>\n",
       "      <td>1.377930</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>13867 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       ECG_ID  Katz_mean_V4  Katz_mean_V5  Katz_mean_V2\n",
       "0      A00002      1.256836      1.205078      1.245117\n",
       "1      A00003      1.423828      1.388672      1.340820\n",
       "2      A00006      1.310547      1.313477      1.375000\n",
       "3      A00008      1.284180      1.262695      1.316406\n",
       "4      A00009      1.292969      1.264648      1.320312\n",
       "...       ...           ...           ...           ...\n",
       "13862  A25755      1.243164      1.220703      1.407227\n",
       "13863  A25756      1.233398      1.227539      1.317383\n",
       "13864  A25757      1.362305      1.320312      1.367188\n",
       "13865  A25764      1.291992      1.266602      1.439453\n",
       "13866  A25765      1.243164      1.222656      1.377930\n",
       "\n",
       "[13867 rows x 4 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Katz_mean_V457"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Conform Katz_mean pickle file formed by the mean of the Katz fractal dimension values for the 12 leads."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "Katz_mean = pd.DataFrame(columns=['ECG_ID','I_mean', 'II_mean', 'III_mean', 'aVR_mean', 'aVL_mean', 'aVF_mean', 'V1_mean', 'V2_mean', 'V3_mean', 'V4_mean', 'V5_mean', 'V6_mean'])\n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "    with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "        try:\n",
    "            Katz_mean.loc[len(Katz_mean)] = [id, f['Katz_fractal'][0,-2], f['Katz_fractal'][1,-2], f['Katz_fractal'][2,-2], f['Katz_fractal'][3,-2], f['Katz_fractal'][4,-2], f['Katz_fractal'][5,-2], f['Katz_fractal'][6,-2], f['Katz_fractal'][7,-2], f['Katz_fractal'][8,-2], f['Katz_fractal'][9,-2], f['Katz_fractal'][10,-2], f['Katz_fractal'][11,-2]]\n",
    "        except IndexError:\n",
    "            continue\n",
    "Katz_mean.to_pickle('Katz_mean.pickle')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ECG_ID</th>\n",
       "      <th>I_mean</th>\n",
       "      <th>II_mean</th>\n",
       "      <th>III_mean</th>\n",
       "      <th>aVR_mean</th>\n",
       "      <th>aVL_mean</th>\n",
       "      <th>aVF_mean</th>\n",
       "      <th>V1_mean</th>\n",
       "      <th>V2_mean</th>\n",
       "      <th>V3_mean</th>\n",
       "      <th>V4_mean</th>\n",
       "      <th>V5_mean</th>\n",
       "      <th>V6_mean</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A00002</td>\n",
       "      <td>1.248047</td>\n",
       "      <td>1.194336</td>\n",
       "      <td>1.248047</td>\n",
       "      <td>1.210938</td>\n",
       "      <td>1.367188</td>\n",
       "      <td>1.192383</td>\n",
       "      <td>1.194336</td>\n",
       "      <td>1.245117</td>\n",
       "      <td>1.377930</td>\n",
       "      <td>1.256836</td>\n",
       "      <td>1.205078</td>\n",
       "      <td>1.176758</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A00003</td>\n",
       "      <td>1.921875</td>\n",
       "      <td>1.493164</td>\n",
       "      <td>1.545898</td>\n",
       "      <td>1.659180</td>\n",
       "      <td>1.859375</td>\n",
       "      <td>1.464844</td>\n",
       "      <td>1.563477</td>\n",
       "      <td>1.340820</td>\n",
       "      <td>1.361328</td>\n",
       "      <td>1.423828</td>\n",
       "      <td>1.388672</td>\n",
       "      <td>1.398438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A00006</td>\n",
       "      <td>1.668945</td>\n",
       "      <td>1.635742</td>\n",
       "      <td>1.791992</td>\n",
       "      <td>1.575195</td>\n",
       "      <td>1.916992</td>\n",
       "      <td>1.703125</td>\n",
       "      <td>1.440430</td>\n",
       "      <td>1.375000</td>\n",
       "      <td>1.408203</td>\n",
       "      <td>1.310547</td>\n",
       "      <td>1.313477</td>\n",
       "      <td>1.321289</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A00008</td>\n",
       "      <td>1.495117</td>\n",
       "      <td>1.263672</td>\n",
       "      <td>1.261719</td>\n",
       "      <td>1.290039</td>\n",
       "      <td>1.298828</td>\n",
       "      <td>1.256836</td>\n",
       "      <td>1.287109</td>\n",
       "      <td>1.316406</td>\n",
       "      <td>1.347656</td>\n",
       "      <td>1.284180</td>\n",
       "      <td>1.262695</td>\n",
       "      <td>1.236328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A00009</td>\n",
       "      <td>1.277344</td>\n",
       "      <td>1.322266</td>\n",
       "      <td>1.553711</td>\n",
       "      <td>1.292969</td>\n",
       "      <td>1.284180</td>\n",
       "      <td>1.416992</td>\n",
       "      <td>1.222656</td>\n",
       "      <td>1.320312</td>\n",
       "      <td>1.398438</td>\n",
       "      <td>1.292969</td>\n",
       "      <td>1.264648</td>\n",
       "      <td>1.253906</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13862</th>\n",
       "      <td>A25755</td>\n",
       "      <td>1.231445</td>\n",
       "      <td>1.244141</td>\n",
       "      <td>1.421875</td>\n",
       "      <td>1.225586</td>\n",
       "      <td>1.266602</td>\n",
       "      <td>1.364258</td>\n",
       "      <td>1.343750</td>\n",
       "      <td>1.407227</td>\n",
       "      <td>1.283203</td>\n",
       "      <td>1.243164</td>\n",
       "      <td>1.220703</td>\n",
       "      <td>1.213867</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13863</th>\n",
       "      <td>A25756</td>\n",
       "      <td>1.351562</td>\n",
       "      <td>1.333008</td>\n",
       "      <td>1.667969</td>\n",
       "      <td>1.324219</td>\n",
       "      <td>1.491211</td>\n",
       "      <td>1.391602</td>\n",
       "      <td>1.355469</td>\n",
       "      <td>1.317383</td>\n",
       "      <td>1.256836</td>\n",
       "      <td>1.233398</td>\n",
       "      <td>1.227539</td>\n",
       "      <td>1.234375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13864</th>\n",
       "      <td>A25757</td>\n",
       "      <td>1.501953</td>\n",
       "      <td>1.314453</td>\n",
       "      <td>1.443359</td>\n",
       "      <td>1.357422</td>\n",
       "      <td>1.698242</td>\n",
       "      <td>1.337891</td>\n",
       "      <td>1.388672</td>\n",
       "      <td>1.367188</td>\n",
       "      <td>1.367188</td>\n",
       "      <td>1.362305</td>\n",
       "      <td>1.320312</td>\n",
       "      <td>1.290039</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13865</th>\n",
       "      <td>A25764</td>\n",
       "      <td>1.392578</td>\n",
       "      <td>1.285156</td>\n",
       "      <td>1.279297</td>\n",
       "      <td>1.320312</td>\n",
       "      <td>1.367188</td>\n",
       "      <td>1.271484</td>\n",
       "      <td>1.397461</td>\n",
       "      <td>1.439453</td>\n",
       "      <td>1.411133</td>\n",
       "      <td>1.291992</td>\n",
       "      <td>1.266602</td>\n",
       "      <td>1.251953</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13866</th>\n",
       "      <td>A25765</td>\n",
       "      <td>1.383789</td>\n",
       "      <td>1.318359</td>\n",
       "      <td>1.347656</td>\n",
       "      <td>1.327148</td>\n",
       "      <td>1.698242</td>\n",
       "      <td>1.323242</td>\n",
       "      <td>1.253906</td>\n",
       "      <td>1.377930</td>\n",
       "      <td>1.409180</td>\n",
       "      <td>1.243164</td>\n",
       "      <td>1.222656</td>\n",
       "      <td>1.221680</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>13867 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       ECG_ID    I_mean   II_mean  III_mean  aVR_mean  aVL_mean  aVF_mean  \\\n",
       "0      A00002  1.248047  1.194336  1.248047  1.210938  1.367188  1.192383   \n",
       "1      A00003  1.921875  1.493164  1.545898  1.659180  1.859375  1.464844   \n",
       "2      A00006  1.668945  1.635742  1.791992  1.575195  1.916992  1.703125   \n",
       "3      A00008  1.495117  1.263672  1.261719  1.290039  1.298828  1.256836   \n",
       "4      A00009  1.277344  1.322266  1.553711  1.292969  1.284180  1.416992   \n",
       "...       ...       ...       ...       ...       ...       ...       ...   \n",
       "13862  A25755  1.231445  1.244141  1.421875  1.225586  1.266602  1.364258   \n",
       "13863  A25756  1.351562  1.333008  1.667969  1.324219  1.491211  1.391602   \n",
       "13864  A25757  1.501953  1.314453  1.443359  1.357422  1.698242  1.337891   \n",
       "13865  A25764  1.392578  1.285156  1.279297  1.320312  1.367188  1.271484   \n",
       "13866  A25765  1.383789  1.318359  1.347656  1.327148  1.698242  1.323242   \n",
       "\n",
       "        V1_mean   V2_mean   V3_mean   V4_mean   V5_mean   V6_mean  \n",
       "0      1.194336  1.245117  1.377930  1.256836  1.205078  1.176758  \n",
       "1      1.563477  1.340820  1.361328  1.423828  1.388672  1.398438  \n",
       "2      1.440430  1.375000  1.408203  1.310547  1.313477  1.321289  \n",
       "3      1.287109  1.316406  1.347656  1.284180  1.262695  1.236328  \n",
       "4      1.222656  1.320312  1.398438  1.292969  1.264648  1.253906  \n",
       "...         ...       ...       ...       ...       ...       ...  \n",
       "13862  1.343750  1.407227  1.283203  1.243164  1.220703  1.213867  \n",
       "13863  1.355469  1.317383  1.256836  1.233398  1.227539  1.234375  \n",
       "13864  1.388672  1.367188  1.367188  1.362305  1.320312  1.290039  \n",
       "13865  1.397461  1.439453  1.411133  1.291992  1.266602  1.251953  \n",
       "13866  1.253906  1.377930  1.409180  1.243164  1.222656  1.221680  \n",
       "\n",
       "[13867 rows x 13 columns]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Katz_mean"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Find the derivations with minor line length fractal dimension standard deviation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "derivations_list_line_length = [] \n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "    with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "        ll = f['Line_length_fractal'][:,-1]\n",
    "        derivations_list_line_length.append(np.argmin(ll))\n",
    "np.save('derivations_list_line_length_std_minimun.npy', derivations_list_line_length)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[   0 2741]\n",
      " [   1 2813]\n",
      " [   2 1168]\n",
      " [   3 3153]\n",
      " [   4 1324]\n",
      " [   5  640]\n",
      " [   6  726]\n",
      " [   7  387]\n",
      " [   8  308]\n",
      " [   9  100]\n",
      " [  10  169]\n",
      " [  11  376]]\n"
     ]
    }
   ],
   "source": [
    "unique, counts = np.unique(derivations_list_line_length, return_counts=True)\n",
    "print(np.asarray((unique, counts)).T)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Derivation ['I', 'II', 'III', 'aVR', 'aVL', 'aVF', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6'] with minor standard deviation: 3 corresponding to aVR, 1 corresponding to II, 0 corresponding to I"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "Line_length_mean_310 = pd.DataFrame(columns=['ECG_ID', 'Ll_mean_aVR', 'Ll_mean_II', 'Ll_mean_I'])\n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "    with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "        try:\n",
    "            Line_length_mean_310.loc[len(Line_length_mean_310)] = [id, f['Line_length_fractal'][3,-2], f['Line_length_fractal'][1,-2], f['Line_length_fractal'][0,-2]]\n",
    "        except IndexError:\n",
    "            continue\n",
    "Line_length_mean_310.to_pickle('Line_length_mean_310.pickle')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ECG_ID</th>\n",
       "      <th>Ll_mean_aVR</th>\n",
       "      <th>Ll_mean_II</th>\n",
       "      <th>Ll_mean_I</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A00002</td>\n",
       "      <td>0.010239</td>\n",
       "      <td>0.010925</td>\n",
       "      <td>0.010254</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A00003</td>\n",
       "      <td>0.014694</td>\n",
       "      <td>0.016937</td>\n",
       "      <td>0.016769</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A00006</td>\n",
       "      <td>0.013832</td>\n",
       "      <td>0.023315</td>\n",
       "      <td>0.009232</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A00008</td>\n",
       "      <td>0.008522</td>\n",
       "      <td>0.013283</td>\n",
       "      <td>0.005756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A00009</td>\n",
       "      <td>0.009232</td>\n",
       "      <td>0.009369</td>\n",
       "      <td>0.009468</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13900</th>\n",
       "      <td>A25755</td>\n",
       "      <td>0.007534</td>\n",
       "      <td>0.006481</td>\n",
       "      <td>0.009285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13901</th>\n",
       "      <td>A25756</td>\n",
       "      <td>0.009590</td>\n",
       "      <td>0.011017</td>\n",
       "      <td>0.009323</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13902</th>\n",
       "      <td>A25757</td>\n",
       "      <td>0.009445</td>\n",
       "      <td>0.011536</td>\n",
       "      <td>0.009064</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13903</th>\n",
       "      <td>A25764</td>\n",
       "      <td>0.013992</td>\n",
       "      <td>0.018799</td>\n",
       "      <td>0.013412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13904</th>\n",
       "      <td>A25765</td>\n",
       "      <td>0.011803</td>\n",
       "      <td>0.016617</td>\n",
       "      <td>0.007889</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>13905 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       ECG_ID  Ll_mean_aVR  Ll_mean_II  Ll_mean_I\n",
       "0      A00002     0.010239    0.010925   0.010254\n",
       "1      A00003     0.014694    0.016937   0.016769\n",
       "2      A00006     0.013832    0.023315   0.009232\n",
       "3      A00008     0.008522    0.013283   0.005756\n",
       "4      A00009     0.009232    0.009369   0.009468\n",
       "...       ...          ...         ...        ...\n",
       "13900  A25755     0.007534    0.006481   0.009285\n",
       "13901  A25756     0.009590    0.011017   0.009323\n",
       "13902  A25757     0.009445    0.011536   0.009064\n",
       "13903  A25764     0.013992    0.018799   0.013412\n",
       "13904  A25765     0.011803    0.016617   0.007889\n",
       "\n",
       "[13905 rows x 4 columns]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Line_length_mean_310"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Conform Line_length_mean pickle file formed by the mean of the line length fractal dimension values for the 12 leads."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "Line_length_mean = pd.DataFrame(columns=['ECG_ID','I_mean', 'II_mean', 'III_mean', 'aVR_mean', 'aVL_mean', 'aVF_mean', 'V1_mean', 'V2_mean', 'V3_mean', 'V4_mean', 'V5_mean', 'V6_mean'])\n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "    with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "        try:\n",
    "            Line_length_mean.loc[len(Line_length_mean)] = [id, f['Line_length_fractal'][0,-2], f['Line_length_fractal'][1,-2], f['Line_length_fractal'][2,-2], f['Line_length_fractal'][3,-2], f['Line_length_fractal'][4,-2], f['Line_length_fractal'][5,-2], f['Line_length_fractal'][6,-2], f['Line_length_fractal'][7,-2], f['Line_length_fractal'][8,-2], f['Line_length_fractal'][9,-2], f['Line_length_fractal'][10,-2], f['Line_length_fractal'][11,-2]]\n",
    "        except IndexError:\n",
    "            continue\n",
    "Line_length_mean.to_pickle('Line_length_mean.pickle')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ECG_ID</th>\n",
       "      <th>I_mean</th>\n",
       "      <th>II_mean</th>\n",
       "      <th>III_mean</th>\n",
       "      <th>aVR_mean</th>\n",
       "      <th>aVL_mean</th>\n",
       "      <th>aVF_mean</th>\n",
       "      <th>V1_mean</th>\n",
       "      <th>V2_mean</th>\n",
       "      <th>V3_mean</th>\n",
       "      <th>V4_mean</th>\n",
       "      <th>V5_mean</th>\n",
       "      <th>V6_mean</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A00002</td>\n",
       "      <td>0.010254</td>\n",
       "      <td>0.010925</td>\n",
       "      <td>0.004246</td>\n",
       "      <td>0.010239</td>\n",
       "      <td>0.005791</td>\n",
       "      <td>0.006756</td>\n",
       "      <td>0.014870</td>\n",
       "      <td>0.029922</td>\n",
       "      <td>0.019150</td>\n",
       "      <td>0.018478</td>\n",
       "      <td>0.017761</td>\n",
       "      <td>0.011131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A00003</td>\n",
       "      <td>0.016769</td>\n",
       "      <td>0.016937</td>\n",
       "      <td>0.017288</td>\n",
       "      <td>0.014694</td>\n",
       "      <td>0.014565</td>\n",
       "      <td>0.014847</td>\n",
       "      <td>0.011406</td>\n",
       "      <td>0.024414</td>\n",
       "      <td>0.025253</td>\n",
       "      <td>0.024124</td>\n",
       "      <td>0.020096</td>\n",
       "      <td>0.016937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A00006</td>\n",
       "      <td>0.009232</td>\n",
       "      <td>0.023315</td>\n",
       "      <td>0.021667</td>\n",
       "      <td>0.013832</td>\n",
       "      <td>0.012016</td>\n",
       "      <td>0.022064</td>\n",
       "      <td>0.010056</td>\n",
       "      <td>0.014465</td>\n",
       "      <td>0.016968</td>\n",
       "      <td>0.019180</td>\n",
       "      <td>0.017471</td>\n",
       "      <td>0.015480</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A00008</td>\n",
       "      <td>0.005756</td>\n",
       "      <td>0.013283</td>\n",
       "      <td>0.011299</td>\n",
       "      <td>0.008522</td>\n",
       "      <td>0.005634</td>\n",
       "      <td>0.012047</td>\n",
       "      <td>0.012917</td>\n",
       "      <td>0.027374</td>\n",
       "      <td>0.024994</td>\n",
       "      <td>0.023941</td>\n",
       "      <td>0.017242</td>\n",
       "      <td>0.012405</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A00009</td>\n",
       "      <td>0.009468</td>\n",
       "      <td>0.009369</td>\n",
       "      <td>0.003212</td>\n",
       "      <td>0.009232</td>\n",
       "      <td>0.005520</td>\n",
       "      <td>0.005161</td>\n",
       "      <td>0.008789</td>\n",
       "      <td>0.011444</td>\n",
       "      <td>0.014862</td>\n",
       "      <td>0.016113</td>\n",
       "      <td>0.014488</td>\n",
       "      <td>0.011269</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13862</th>\n",
       "      <td>A25755</td>\n",
       "      <td>0.009285</td>\n",
       "      <td>0.006481</td>\n",
       "      <td>0.005634</td>\n",
       "      <td>0.007534</td>\n",
       "      <td>0.006992</td>\n",
       "      <td>0.003202</td>\n",
       "      <td>0.006989</td>\n",
       "      <td>0.015434</td>\n",
       "      <td>0.018311</td>\n",
       "      <td>0.016098</td>\n",
       "      <td>0.012878</td>\n",
       "      <td>0.010254</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13863</th>\n",
       "      <td>A25756</td>\n",
       "      <td>0.009323</td>\n",
       "      <td>0.011017</td>\n",
       "      <td>0.006641</td>\n",
       "      <td>0.009590</td>\n",
       "      <td>0.005753</td>\n",
       "      <td>0.008018</td>\n",
       "      <td>0.006855</td>\n",
       "      <td>0.007820</td>\n",
       "      <td>0.011147</td>\n",
       "      <td>0.013496</td>\n",
       "      <td>0.012627</td>\n",
       "      <td>0.010399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13864</th>\n",
       "      <td>A25757</td>\n",
       "      <td>0.009064</td>\n",
       "      <td>0.011536</td>\n",
       "      <td>0.009926</td>\n",
       "      <td>0.009445</td>\n",
       "      <td>0.006676</td>\n",
       "      <td>0.009850</td>\n",
       "      <td>0.013321</td>\n",
       "      <td>0.021484</td>\n",
       "      <td>0.018372</td>\n",
       "      <td>0.017136</td>\n",
       "      <td>0.018127</td>\n",
       "      <td>0.013878</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13865</th>\n",
       "      <td>A25764</td>\n",
       "      <td>0.013412</td>\n",
       "      <td>0.018799</td>\n",
       "      <td>0.015404</td>\n",
       "      <td>0.013992</td>\n",
       "      <td>0.010849</td>\n",
       "      <td>0.016113</td>\n",
       "      <td>0.008598</td>\n",
       "      <td>0.017548</td>\n",
       "      <td>0.019196</td>\n",
       "      <td>0.022369</td>\n",
       "      <td>0.020538</td>\n",
       "      <td>0.017288</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13866</th>\n",
       "      <td>A25765</td>\n",
       "      <td>0.007889</td>\n",
       "      <td>0.016617</td>\n",
       "      <td>0.011116</td>\n",
       "      <td>0.011803</td>\n",
       "      <td>0.005238</td>\n",
       "      <td>0.013496</td>\n",
       "      <td>0.009346</td>\n",
       "      <td>0.016937</td>\n",
       "      <td>0.013985</td>\n",
       "      <td>0.020401</td>\n",
       "      <td>0.019150</td>\n",
       "      <td>0.015327</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>13867 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       ECG_ID    I_mean   II_mean  III_mean  aVR_mean  aVL_mean  aVF_mean  \\\n",
       "0      A00002  0.010254  0.010925  0.004246  0.010239  0.005791  0.006756   \n",
       "1      A00003  0.016769  0.016937  0.017288  0.014694  0.014565  0.014847   \n",
       "2      A00006  0.009232  0.023315  0.021667  0.013832  0.012016  0.022064   \n",
       "3      A00008  0.005756  0.013283  0.011299  0.008522  0.005634  0.012047   \n",
       "4      A00009  0.009468  0.009369  0.003212  0.009232  0.005520  0.005161   \n",
       "...       ...       ...       ...       ...       ...       ...       ...   \n",
       "13862  A25755  0.009285  0.006481  0.005634  0.007534  0.006992  0.003202   \n",
       "13863  A25756  0.009323  0.011017  0.006641  0.009590  0.005753  0.008018   \n",
       "13864  A25757  0.009064  0.011536  0.009926  0.009445  0.006676  0.009850   \n",
       "13865  A25764  0.013412  0.018799  0.015404  0.013992  0.010849  0.016113   \n",
       "13866  A25765  0.007889  0.016617  0.011116  0.011803  0.005238  0.013496   \n",
       "\n",
       "        V1_mean   V2_mean   V3_mean   V4_mean   V5_mean   V6_mean  \n",
       "0      0.014870  0.029922  0.019150  0.018478  0.017761  0.011131  \n",
       "1      0.011406  0.024414  0.025253  0.024124  0.020096  0.016937  \n",
       "2      0.010056  0.014465  0.016968  0.019180  0.017471  0.015480  \n",
       "3      0.012917  0.027374  0.024994  0.023941  0.017242  0.012405  \n",
       "4      0.008789  0.011444  0.014862  0.016113  0.014488  0.011269  \n",
       "...         ...       ...       ...       ...       ...       ...  \n",
       "13862  0.006989  0.015434  0.018311  0.016098  0.012878  0.010254  \n",
       "13863  0.006855  0.007820  0.011147  0.013496  0.012627  0.010399  \n",
       "13864  0.013321  0.021484  0.018372  0.017136  0.018127  0.013878  \n",
       "13865  0.008598  0.017548  0.019196  0.022369  0.020538  0.017288  \n",
       "13866  0.009346  0.016937  0.013985  0.020401  0.019150  0.015327  \n",
       "\n",
       "[13867 rows x 13 columns]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Line_length_mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "Katz_mean = pd.read_pickle('Katz_mean.pickle')\n",
    "Line_length_mean = pd.read_pickle('Line_length_mean.pickle')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "Katz_Line_length_mean = pd.merge(Katz_mean, Line_length_mean, on=['ECG_ID', 'ECG_ID'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "Katz_Line_length_mean = pd.merge(Katz_Line_length_mean, normal_ecg_age, on=['ECG_ID', 'ECG_ID'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>I_mean_x</th>\n",
       "      <th>II_mean_x</th>\n",
       "      <th>III_mean_x</th>\n",
       "      <th>aVR_mean_x</th>\n",
       "      <th>aVL_mean_x</th>\n",
       "      <th>aVF_mean_x</th>\n",
       "      <th>V1_mean_x</th>\n",
       "      <th>V2_mean_x</th>\n",
       "      <th>V3_mean_x</th>\n",
       "      <th>V4_mean_x</th>\n",
       "      <th>...</th>\n",
       "      <th>V2_mean_y</th>\n",
       "      <th>V3_mean_y</th>\n",
       "      <th>V4_mean_y</th>\n",
       "      <th>V5_mean_y</th>\n",
       "      <th>V6_mean_y</th>\n",
       "      <th>Age</th>\n",
       "      <th>Age_class_0</th>\n",
       "      <th>Age_class_1</th>\n",
       "      <th>Age_class_2</th>\n",
       "      <th>Age_class_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.248047</td>\n",
       "      <td>1.194336</td>\n",
       "      <td>1.248047</td>\n",
       "      <td>1.210938</td>\n",
       "      <td>1.367188</td>\n",
       "      <td>1.192383</td>\n",
       "      <td>1.194336</td>\n",
       "      <td>1.245117</td>\n",
       "      <td>1.377930</td>\n",
       "      <td>1.256836</td>\n",
       "      <td>...</td>\n",
       "      <td>0.029922</td>\n",
       "      <td>0.019150</td>\n",
       "      <td>0.018478</td>\n",
       "      <td>0.017761</td>\n",
       "      <td>0.011131</td>\n",
       "      <td>32</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.921875</td>\n",
       "      <td>1.493164</td>\n",
       "      <td>1.545898</td>\n",
       "      <td>1.659180</td>\n",
       "      <td>1.859375</td>\n",
       "      <td>1.464844</td>\n",
       "      <td>1.563477</td>\n",
       "      <td>1.340820</td>\n",
       "      <td>1.361328</td>\n",
       "      <td>1.423828</td>\n",
       "      <td>...</td>\n",
       "      <td>0.024414</td>\n",
       "      <td>0.025253</td>\n",
       "      <td>0.024124</td>\n",
       "      <td>0.020096</td>\n",
       "      <td>0.016937</td>\n",
       "      <td>63</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.668945</td>\n",
       "      <td>1.635742</td>\n",
       "      <td>1.791992</td>\n",
       "      <td>1.575195</td>\n",
       "      <td>1.916992</td>\n",
       "      <td>1.703125</td>\n",
       "      <td>1.440430</td>\n",
       "      <td>1.375000</td>\n",
       "      <td>1.408203</td>\n",
       "      <td>1.310547</td>\n",
       "      <td>...</td>\n",
       "      <td>0.014465</td>\n",
       "      <td>0.016968</td>\n",
       "      <td>0.019180</td>\n",
       "      <td>0.017471</td>\n",
       "      <td>0.015480</td>\n",
       "      <td>46</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.495117</td>\n",
       "      <td>1.263672</td>\n",
       "      <td>1.261719</td>\n",
       "      <td>1.290039</td>\n",
       "      <td>1.298828</td>\n",
       "      <td>1.256836</td>\n",
       "      <td>1.287109</td>\n",
       "      <td>1.316406</td>\n",
       "      <td>1.347656</td>\n",
       "      <td>1.284180</td>\n",
       "      <td>...</td>\n",
       "      <td>0.027374</td>\n",
       "      <td>0.024994</td>\n",
       "      <td>0.023941</td>\n",
       "      <td>0.017242</td>\n",
       "      <td>0.012405</td>\n",
       "      <td>32</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.277344</td>\n",
       "      <td>1.322266</td>\n",
       "      <td>1.553711</td>\n",
       "      <td>1.292969</td>\n",
       "      <td>1.284180</td>\n",
       "      <td>1.416992</td>\n",
       "      <td>1.222656</td>\n",
       "      <td>1.320312</td>\n",
       "      <td>1.398438</td>\n",
       "      <td>1.292969</td>\n",
       "      <td>...</td>\n",
       "      <td>0.011444</td>\n",
       "      <td>0.014862</td>\n",
       "      <td>0.016113</td>\n",
       "      <td>0.014488</td>\n",
       "      <td>0.011269</td>\n",
       "      <td>48</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13862</th>\n",
       "      <td>1.231445</td>\n",
       "      <td>1.244141</td>\n",
       "      <td>1.421875</td>\n",
       "      <td>1.225586</td>\n",
       "      <td>1.266602</td>\n",
       "      <td>1.364258</td>\n",
       "      <td>1.343750</td>\n",
       "      <td>1.407227</td>\n",
       "      <td>1.283203</td>\n",
       "      <td>1.243164</td>\n",
       "      <td>...</td>\n",
       "      <td>0.015434</td>\n",
       "      <td>0.018311</td>\n",
       "      <td>0.016098</td>\n",
       "      <td>0.012878</td>\n",
       "      <td>0.010254</td>\n",
       "      <td>44</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13863</th>\n",
       "      <td>1.351562</td>\n",
       "      <td>1.333008</td>\n",
       "      <td>1.667969</td>\n",
       "      <td>1.324219</td>\n",
       "      <td>1.491211</td>\n",
       "      <td>1.391602</td>\n",
       "      <td>1.355469</td>\n",
       "      <td>1.317383</td>\n",
       "      <td>1.256836</td>\n",
       "      <td>1.233398</td>\n",
       "      <td>...</td>\n",
       "      <td>0.007820</td>\n",
       "      <td>0.011147</td>\n",
       "      <td>0.013496</td>\n",
       "      <td>0.012627</td>\n",
       "      <td>0.010399</td>\n",
       "      <td>76</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13864</th>\n",
       "      <td>1.501953</td>\n",
       "      <td>1.314453</td>\n",
       "      <td>1.443359</td>\n",
       "      <td>1.357422</td>\n",
       "      <td>1.698242</td>\n",
       "      <td>1.337891</td>\n",
       "      <td>1.388672</td>\n",
       "      <td>1.367188</td>\n",
       "      <td>1.367188</td>\n",
       "      <td>1.362305</td>\n",
       "      <td>...</td>\n",
       "      <td>0.021484</td>\n",
       "      <td>0.018372</td>\n",
       "      <td>0.017136</td>\n",
       "      <td>0.018127</td>\n",
       "      <td>0.013878</td>\n",
       "      <td>55</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13865</th>\n",
       "      <td>1.392578</td>\n",
       "      <td>1.285156</td>\n",
       "      <td>1.279297</td>\n",
       "      <td>1.320312</td>\n",
       "      <td>1.367188</td>\n",
       "      <td>1.271484</td>\n",
       "      <td>1.397461</td>\n",
       "      <td>1.439453</td>\n",
       "      <td>1.411133</td>\n",
       "      <td>1.291992</td>\n",
       "      <td>...</td>\n",
       "      <td>0.017548</td>\n",
       "      <td>0.019196</td>\n",
       "      <td>0.022369</td>\n",
       "      <td>0.020538</td>\n",
       "      <td>0.017288</td>\n",
       "      <td>20</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13866</th>\n",
       "      <td>1.383789</td>\n",
       "      <td>1.318359</td>\n",
       "      <td>1.347656</td>\n",
       "      <td>1.327148</td>\n",
       "      <td>1.698242</td>\n",
       "      <td>1.323242</td>\n",
       "      <td>1.253906</td>\n",
       "      <td>1.377930</td>\n",
       "      <td>1.409180</td>\n",
       "      <td>1.243164</td>\n",
       "      <td>...</td>\n",
       "      <td>0.016937</td>\n",
       "      <td>0.013985</td>\n",
       "      <td>0.020401</td>\n",
       "      <td>0.019150</td>\n",
       "      <td>0.015327</td>\n",
       "      <td>52</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>13867 rows × 29 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       I_mean_x  II_mean_x  III_mean_x  aVR_mean_x  aVL_mean_x  aVF_mean_x  \\\n",
       "0      1.248047   1.194336    1.248047    1.210938    1.367188    1.192383   \n",
       "1      1.921875   1.493164    1.545898    1.659180    1.859375    1.464844   \n",
       "2      1.668945   1.635742    1.791992    1.575195    1.916992    1.703125   \n",
       "3      1.495117   1.263672    1.261719    1.290039    1.298828    1.256836   \n",
       "4      1.277344   1.322266    1.553711    1.292969    1.284180    1.416992   \n",
       "...         ...        ...         ...         ...         ...         ...   \n",
       "13862  1.231445   1.244141    1.421875    1.225586    1.266602    1.364258   \n",
       "13863  1.351562   1.333008    1.667969    1.324219    1.491211    1.391602   \n",
       "13864  1.501953   1.314453    1.443359    1.357422    1.698242    1.337891   \n",
       "13865  1.392578   1.285156    1.279297    1.320312    1.367188    1.271484   \n",
       "13866  1.383789   1.318359    1.347656    1.327148    1.698242    1.323242   \n",
       "\n",
       "       V1_mean_x  V2_mean_x  V3_mean_x  V4_mean_x  ...  V2_mean_y  V3_mean_y  \\\n",
       "0       1.194336   1.245117   1.377930   1.256836  ...   0.029922   0.019150   \n",
       "1       1.563477   1.340820   1.361328   1.423828  ...   0.024414   0.025253   \n",
       "2       1.440430   1.375000   1.408203   1.310547  ...   0.014465   0.016968   \n",
       "3       1.287109   1.316406   1.347656   1.284180  ...   0.027374   0.024994   \n",
       "4       1.222656   1.320312   1.398438   1.292969  ...   0.011444   0.014862   \n",
       "...          ...        ...        ...        ...  ...        ...        ...   \n",
       "13862   1.343750   1.407227   1.283203   1.243164  ...   0.015434   0.018311   \n",
       "13863   1.355469   1.317383   1.256836   1.233398  ...   0.007820   0.011147   \n",
       "13864   1.388672   1.367188   1.367188   1.362305  ...   0.021484   0.018372   \n",
       "13865   1.397461   1.439453   1.411133   1.291992  ...   0.017548   0.019196   \n",
       "13866   1.253906   1.377930   1.409180   1.243164  ...   0.016937   0.013985   \n",
       "\n",
       "       V4_mean_y  V5_mean_y  V6_mean_y  Age  Age_class_0  Age_class_1  \\\n",
       "0       0.018478   0.017761   0.011131   32            2            1   \n",
       "1       0.024124   0.020096   0.016937   63            5            2   \n",
       "2       0.019180   0.017471   0.015480   46            3            1   \n",
       "3       0.023941   0.017242   0.012405   32            2            1   \n",
       "4       0.016113   0.014488   0.011269   48            3            1   \n",
       "...          ...        ...        ...  ...          ...          ...   \n",
       "13862   0.016098   0.012878   0.010254   44            3            1   \n",
       "13863   0.013496   0.012627   0.010399   76            6            3   \n",
       "13864   0.017136   0.018127   0.013878   55            4            2   \n",
       "13865   0.022369   0.020538   0.017288   20            1            0   \n",
       "13866   0.020401   0.019150   0.015327   52            4            2   \n",
       "\n",
       "       Age_class_2  Age_class_3  \n",
       "0                0            0  \n",
       "1                1            1  \n",
       "2                1            0  \n",
       "3                0            0  \n",
       "4                1            0  \n",
       "...            ...          ...  \n",
       "13862            1            0  \n",
       "13863            2            1  \n",
       "13864            1            1  \n",
       "13865            0            0  \n",
       "13866            1            1  \n",
       "\n",
       "[13867 rows x 29 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Katz_Line_length_mean = Katz_Line_length_mean.drop('ECG_ID', axis=1)\n",
    "Katz_Line_length_mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(Katz_Line_length_mean.iloc[:,:24], Katz_Line_length_mean['Age_class_1'], random_state=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test set score: 0.52\n"
     ]
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "knn = KNeighborsClassifier(n_neighbors=25)\n",
    "knn.fit(X_train, y_train)\n",
    "print(\"Test set score: {:.2f}\".format(knn.score(X_test, y_test)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy on training set: 1.00\n",
      "Accuracy on test set: 0.44\n"
     ]
    }
   ],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "tree = DecisionTreeClassifier(random_state=0)\n",
    "tree.fit(X_train, y_train)\n",
    "print(\"Accuracy on training set: {:.2f}\".format(tree.score(X_train, y_train)))\n",
    "print(\"Accuracy on test set: {:.2f}\".format(tree.score(X_test, y_test)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Find the derivations with minor Petrosian fractal dimension standard deviation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "derivations_list_Petrosian = [] \n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "    with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "        p = f['Petrosian_fractal'][:,-1]\n",
    "        derivations_list_Petrosian.append(np.argmin(p))\n",
    "np.save('derivations_list_Petrosian_std_minimun.npy', derivations_list_Petrosian)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[   0  569]\n",
      " [   1  955]\n",
      " [   2  924]\n",
      " [   3  664]\n",
      " [   4  652]\n",
      " [   5  871]\n",
      " [   6  637]\n",
      " [   7 2004]\n",
      " [   8 2174]\n",
      " [   9 2111]\n",
      " [  10 1460]\n",
      " [  11  884]]\n"
     ]
    }
   ],
   "source": [
    "unique, counts = np.unique(derivations_list_Petrosian, return_counts=True)\n",
    "print(np.asarray((unique, counts)).T)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Derivation ['I', 'II', 'III', 'aVR', 'aVL', 'aVF', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6'] with minor standard deviation: 8 corresponding to V3, 9 corresponding to V4, 10 corresponding to V5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "Petrosian_mean_8910 = pd.DataFrame(columns=['ECG_ID', 'Petrosian_mean_V3', 'Petrosian_mean_V4', 'Petrosian_mean_V5'])\n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "    with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "        try:\n",
    "            Petrosian_mean_8910.loc[len(Petrosian_mean_8910)] = [id, f['Petrosian_fractal'][8,-2], f['Petrosian_fractal'][9,-2], f['Petrosian_fractal'][10,-2]]\n",
    "        except IndexError:\n",
    "            continue\n",
    "Petrosian_mean_8910.to_pickle('Petrosian_mean_8910.pickle')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Conform Petrosian_mean pickle file formed by the mean of the Petrosian fractal dimension values for the 12 leads."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "Petrosian_mean = pd.DataFrame(columns=['ECG_ID','I_mean', 'II_mean', 'III_mean', 'aVR_mean', 'aVL_mean', 'aVF_mean', 'V1_mean', 'V2_mean', 'V3_mean', 'V4_mean', 'V5_mean', 'V6_mean'])\n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "    with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "        try:\n",
    "            Petrosian_mean.loc[len(Petrosian_mean)] = [id, f['Petrosian_fractal'][0,-2], f['Petrosian_fractal'][1,-2], f['Petrosian_fractal'][2,-2], f['Petrosian_fractal'][3,-2], f['Petrosian_fractal'][4,-2], f['Petrosian_fractal'][5,-2], f['Petrosian_fractal'][6,-2], f['Petrosian_fractal'][7,-2], f['Petrosian_fractal'][8,-2], f['Petrosian_fractal'][9,-2], f['Petrosian_fractal'][10,-2], f['Petrosian_fractal'][11,-2]]\n",
    "        except IndexError:\n",
    "            continue\n",
    "Petrosian_mean.to_pickle('Petrosian_mean.pickle')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ECG_ID</th>\n",
       "      <th>I_mean</th>\n",
       "      <th>II_mean</th>\n",
       "      <th>III_mean</th>\n",
       "      <th>aVR_mean</th>\n",
       "      <th>aVL_mean</th>\n",
       "      <th>aVF_mean</th>\n",
       "      <th>V1_mean</th>\n",
       "      <th>V2_mean</th>\n",
       "      <th>V3_mean</th>\n",
       "      <th>V4_mean</th>\n",
       "      <th>V5_mean</th>\n",
       "      <th>V6_mean</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A00002</td>\n",
       "      <td>1.007326</td>\n",
       "      <td>1.009275</td>\n",
       "      <td>1.008594</td>\n",
       "      <td>1.008416</td>\n",
       "      <td>1.006542</td>\n",
       "      <td>1.011021</td>\n",
       "      <td>1.006702</td>\n",
       "      <td>1.004257</td>\n",
       "      <td>1.003679</td>\n",
       "      <td>1.004223</td>\n",
       "      <td>1.004620</td>\n",
       "      <td>1.007488</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A00003</td>\n",
       "      <td>1.030798</td>\n",
       "      <td>1.029596</td>\n",
       "      <td>1.029637</td>\n",
       "      <td>1.031062</td>\n",
       "      <td>1.030363</td>\n",
       "      <td>1.028488</td>\n",
       "      <td>1.028260</td>\n",
       "      <td>1.021499</td>\n",
       "      <td>1.020602</td>\n",
       "      <td>1.023961</td>\n",
       "      <td>1.025274</td>\n",
       "      <td>1.025216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A00006</td>\n",
       "      <td>1.034390</td>\n",
       "      <td>1.034431</td>\n",
       "      <td>1.034681</td>\n",
       "      <td>1.034035</td>\n",
       "      <td>1.035199</td>\n",
       "      <td>1.034495</td>\n",
       "      <td>1.033017</td>\n",
       "      <td>1.029001</td>\n",
       "      <td>1.028135</td>\n",
       "      <td>1.028961</td>\n",
       "      <td>1.029256</td>\n",
       "      <td>1.030291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A00008</td>\n",
       "      <td>1.009951</td>\n",
       "      <td>1.010471</td>\n",
       "      <td>1.011870</td>\n",
       "      <td>1.010562</td>\n",
       "      <td>1.012878</td>\n",
       "      <td>1.011194</td>\n",
       "      <td>1.008181</td>\n",
       "      <td>1.006584</td>\n",
       "      <td>1.006790</td>\n",
       "      <td>1.007726</td>\n",
       "      <td>1.007680</td>\n",
       "      <td>1.008432</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A00009</td>\n",
       "      <td>1.009022</td>\n",
       "      <td>1.009048</td>\n",
       "      <td>1.011518</td>\n",
       "      <td>1.008859</td>\n",
       "      <td>1.010589</td>\n",
       "      <td>1.009477</td>\n",
       "      <td>1.008282</td>\n",
       "      <td>1.006249</td>\n",
       "      <td>1.006124</td>\n",
       "      <td>1.006495</td>\n",
       "      <td>1.006889</td>\n",
       "      <td>1.007268</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13862</th>\n",
       "      <td>A25755</td>\n",
       "      <td>1.011745</td>\n",
       "      <td>1.011046</td>\n",
       "      <td>1.014578</td>\n",
       "      <td>1.010887</td>\n",
       "      <td>1.013144</td>\n",
       "      <td>1.012580</td>\n",
       "      <td>1.010169</td>\n",
       "      <td>1.007018</td>\n",
       "      <td>1.009613</td>\n",
       "      <td>1.009902</td>\n",
       "      <td>1.010215</td>\n",
       "      <td>1.009665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13863</th>\n",
       "      <td>A25756</td>\n",
       "      <td>1.015982</td>\n",
       "      <td>1.016773</td>\n",
       "      <td>1.015599</td>\n",
       "      <td>1.016335</td>\n",
       "      <td>1.015923</td>\n",
       "      <td>1.016054</td>\n",
       "      <td>1.012233</td>\n",
       "      <td>1.013155</td>\n",
       "      <td>1.013750</td>\n",
       "      <td>1.013018</td>\n",
       "      <td>1.012713</td>\n",
       "      <td>1.013334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13864</th>\n",
       "      <td>A25757</td>\n",
       "      <td>1.011780</td>\n",
       "      <td>1.009568</td>\n",
       "      <td>1.014153</td>\n",
       "      <td>1.011081</td>\n",
       "      <td>1.013707</td>\n",
       "      <td>1.012540</td>\n",
       "      <td>1.009997</td>\n",
       "      <td>1.009137</td>\n",
       "      <td>1.010586</td>\n",
       "      <td>1.010923</td>\n",
       "      <td>1.009953</td>\n",
       "      <td>1.010338</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13865</th>\n",
       "      <td>A25764</td>\n",
       "      <td>1.010270</td>\n",
       "      <td>1.012229</td>\n",
       "      <td>1.013551</td>\n",
       "      <td>1.011307</td>\n",
       "      <td>1.012790</td>\n",
       "      <td>1.012610</td>\n",
       "      <td>1.012587</td>\n",
       "      <td>1.008182</td>\n",
       "      <td>1.008797</td>\n",
       "      <td>1.008888</td>\n",
       "      <td>1.009568</td>\n",
       "      <td>1.009818</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13866</th>\n",
       "      <td>A25765</td>\n",
       "      <td>1.009757</td>\n",
       "      <td>1.009226</td>\n",
       "      <td>1.009772</td>\n",
       "      <td>1.009436</td>\n",
       "      <td>1.011243</td>\n",
       "      <td>1.009546</td>\n",
       "      <td>1.007482</td>\n",
       "      <td>1.006205</td>\n",
       "      <td>1.006492</td>\n",
       "      <td>1.006721</td>\n",
       "      <td>1.006835</td>\n",
       "      <td>1.006719</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>13867 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       ECG_ID    I_mean   II_mean  III_mean  aVR_mean  aVL_mean  aVF_mean  \\\n",
       "0      A00002  1.007326  1.009275  1.008594  1.008416  1.006542  1.011021   \n",
       "1      A00003  1.030798  1.029596  1.029637  1.031062  1.030363  1.028488   \n",
       "2      A00006  1.034390  1.034431  1.034681  1.034035  1.035199  1.034495   \n",
       "3      A00008  1.009951  1.010471  1.011870  1.010562  1.012878  1.011194   \n",
       "4      A00009  1.009022  1.009048  1.011518  1.008859  1.010589  1.009477   \n",
       "...       ...       ...       ...       ...       ...       ...       ...   \n",
       "13862  A25755  1.011745  1.011046  1.014578  1.010887  1.013144  1.012580   \n",
       "13863  A25756  1.015982  1.016773  1.015599  1.016335  1.015923  1.016054   \n",
       "13864  A25757  1.011780  1.009568  1.014153  1.011081  1.013707  1.012540   \n",
       "13865  A25764  1.010270  1.012229  1.013551  1.011307  1.012790  1.012610   \n",
       "13866  A25765  1.009757  1.009226  1.009772  1.009436  1.011243  1.009546   \n",
       "\n",
       "        V1_mean   V2_mean   V3_mean   V4_mean   V5_mean   V6_mean  \n",
       "0      1.006702  1.004257  1.003679  1.004223  1.004620  1.007488  \n",
       "1      1.028260  1.021499  1.020602  1.023961  1.025274  1.025216  \n",
       "2      1.033017  1.029001  1.028135  1.028961  1.029256  1.030291  \n",
       "3      1.008181  1.006584  1.006790  1.007726  1.007680  1.008432  \n",
       "4      1.008282  1.006249  1.006124  1.006495  1.006889  1.007268  \n",
       "...         ...       ...       ...       ...       ...       ...  \n",
       "13862  1.010169  1.007018  1.009613  1.009902  1.010215  1.009665  \n",
       "13863  1.012233  1.013155  1.013750  1.013018  1.012713  1.013334  \n",
       "13864  1.009997  1.009137  1.010586  1.010923  1.009953  1.010338  \n",
       "13865  1.012587  1.008182  1.008797  1.008888  1.009568  1.009818  \n",
       "13866  1.007482  1.006205  1.006492  1.006721  1.006835  1.006719  \n",
       "\n",
       "[13867 rows x 13 columns]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Petrosian_mean                                                                                                                                                                                                                                                                                                                                                                                  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Find the derivations with minor Sevcik fractal dimension standard deviation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "derivations_list_Sevcik = [] \n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "    with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "        s = f['Sevcik_fractal'][:,-1]\n",
    "        derivations_list_Sevcik.append(np.argmin(s))\n",
    "np.save('derivations_list_Sevcik_std_minimun.npy', derivations_list_Sevcik)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[   0  399]\n",
      " [   1  420]\n",
      " [   2  152]\n",
      " [   3  365]\n",
      " [   4   81]\n",
      " [   5  217]\n",
      " [   6  627]\n",
      " [   7 2155]\n",
      " [   8 2677]\n",
      " [   9 3355]\n",
      " [  10 2477]\n",
      " [  11  980]]\n"
     ]
    }
   ],
   "source": [
    "unique, counts = np.unique(derivations_list_Sevcik, return_counts=True)\n",
    "print(np.asarray((unique, counts)).T)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Derivation ['I', 'II', 'III', 'aVR', 'aVL', 'aVF', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6'] with minor standard deviation: 9 corresponding to V4, 8 corresponding to V3, 10 corresponding to V5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "Sevcik_mean_9810 = pd.DataFrame(columns=['ECG_ID', 'Sevcik_mean_V4', 'Sevcik_mean_V3', 'Sevcik_mean_V5'])\n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "    with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "        try:\n",
    "            Sevcik_mean_9810.loc[len(Sevcik_mean_9810)] = [id, f['Sevcik_fractal'][9,-2], f['Sevcik_fractal'][8,-2], f['Sevcik_fractal'][10,-2]]\n",
    "        except IndexError:\n",
    "            continue\n",
    "Sevcik_mean_9810.to_pickle('Sevcik_mean_9810.pickle')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Conform Sevcik_mean pickle file formed by the mean of the Sevcik fractal dimension values for the 12 leads."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "Sevcik_mean = pd.DataFrame(columns=['ECG_ID','I_mean', 'II_mean', 'III_mean', 'aVR_mean', 'aVL_mean', 'aVF_mean', 'V1_mean', 'V2_mean', 'V3_mean', 'V4_mean', 'V5_mean', 'V6_mean'])\n",
    "for id in normal_ecg_age['ECG_ID']:\n",
    "    with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "        try:\n",
    "            Sevcik_mean.loc[len(Sevcik_mean)] = [id, f['Sevcik_fractal'][0,-2], f['Sevcik_fractal'][1,-2], f['Sevcik_fractal'][2,-2], f['Sevcik_fractal'][3,-2], f['Sevcik_fractal'][4,-2], f['Sevcik_fractal'][5,-2], f['Sevcik_fractal'][6,-2], f['Sevcik_fractal'][7,-2], f['Sevcik_fractal'][8,-2], f['Sevcik_fractal'][9,-2], f['Sevcik_fractal'][10,-2], f['Sevcik_fractal'][11,-2]]\n",
    "        except IndexError:\n",
    "            continue\n",
    "Sevcik_mean.to_pickle('Sevcik_mean.pickle')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "for id in normal_ecg_age['ECG_ID']:\n",
    "        with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "                f.create_group('Fractal_Dimension')\n",
    "                for member in f.keys():\n",
    "                        if (member!='Fractal_Dimension' and member!='ecg' and member!='ECG_R_Peaks'):\n",
    "                                f.move(f'{member}', f'Fractal_Dimension/{member}')\n",
    "                                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "with h5py.File('E:/1-DENIS/Biomarkers/SPH dataset/records/A00002.h5', 'r+') as f:\n",
    "    for member in f['Fractal_Dimension'].keys():\n",
    "        f.move(f'Fractal_Dimension/{member}',f'{member}')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "with h5py.File('E:/1-DENIS/Biomarkers/SPH dataset/records/A00002.h5', 'r+') as f:\n",
    "    del f['Fractal_Dimension/fractal_higuchi']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "for id in normal_ecg_age['ECG_ID']:\n",
    "    with h5py.File(f'E:/1-DENIS/Biomarkers/SPH dataset/records/{id}.h5', 'r+') as f:\n",
    "        f['Fractal_Dimension/fractal_nld'] = f['Fractal_Dimension/NLD_fractal']\n",
    "        del f['Fractal_Dimension/NLD_fractal']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "fractal_psdslope - error\n",
    "fractal_higuchi\n",
    "fractal_density - work in progress\n",
    "fractal_hurst\n",
    "fractal_correlation\n",
    "fractal_dfa\n",
    "fractal_tmf"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.5 ('ecg_biomarkers': venv)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d39f5264c2f26cb0964d5a98dbbf3a9d2bc7301339817434e4f0ffa82f5f047f"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
